package GUI;




//package scut;


import java.awt.*;                                                                   
import java.awt.event.*;   
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat; 

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;


class WindowButton extends Frame implements ActionListener {
	int number;
	int score;
	int counter = 0;
	Label label;// 提示条
	TextField textField;// 输入框
	Button buttonGetNumber, buttonEnter,buttonExit;
	 long startTime=System.currentTimeMillis();
	 long endTime;
	
	
	
	public WindowButton() {
		setLayout(new FlowLayout());
		buttonGetNumber = new Button("得到一个0-100随机数");
		add(buttonGetNumber);
		label = new Label("输入你的猜测", Label.CENTER);
		label.setBackground(Color.CYAN);
		textField = new TextField("0", 10);
		add(label);
		add(textField,BorderLayout.CENTER);
		buttonEnter = new Button("确定");
		add(buttonEnter);
		buttonExit=new Button("退出") ;
		add(buttonExit);
		buttonEnter.addActionListener(this);
		buttonGetNumber.addActionListener(this);
		setBounds(100, 100, 200, 200);
		setResizable(true);//窗口大小可改变
		setSize(300,100);//框架大小
		setLocationRelativeTo(null);// 在显示器居中显示
		addWindowListener(new WindowAdapter() {
			/* 使用匿名类添加一个窗口监听器 */
			public void windowClosing(WindowEvent e) {
				System.exit(0);//退出应用程序
			}
			public void windowActivated(WindowEvent e){
				setTitle("猜数字游戏");//改变窗口标题
			}
		});
		setVisible(true);
		validate();
	}
	
	

	

	public void actionPerformed(ActionEvent e) {
		counter++;
		if (e.getSource() == buttonGetNumber) {
			long startTime = System.currentTimeMillis();
			number = (int) (Math.random() * 100 + 1);
			label.setText("输入0-100你的猜测");
		}else if(e.getSource()==buttonEnter){
			int guess = 0;
			try {
				guess = Integer.parseInt(textField.getText());
			}catch (NumberFormatException event) {
				JOptionPane.showMessageDialog(null, "数字不合法，请输入一个1-100之间的整数!"); 
				label.setText("请重新输入");
			}
				if (guess > number) {
					label.setText("你猜大了");
					textField.setText(null);
				}
				if (guess < number) {
					label.setText("你猜小了");
					textField.setText(null);
				}
				if (guess == number) {
					long endTime = System.currentTimeMillis();                                                                                  
					switch(counter){                                                                                                     
					case 1:JOptionPane.showMessageDialog(null,"You are a man of god!!");                                                 
					break;                                                                                                               
					case 2:                                                                                                              
					case 3:                                                                                                              
					case 4:JOptionPane.showMessageDialog(null,"You are great!");                                                         
					break;                                                                                                               
					case 5:                                                                                                              
					case 6:                                                                                                              
					case 7:JOptionPane.showMessageDialog(null,"You are a little lame");                                                  
					break;                                                                                                               
					case 8:                                                                                                              
					case 9:                                                                                                              
					default:JOptionPane.showMessageDialog(null,"It's not good!you have to work hard!!");                                 
					break;                                                                                                               
					}          
					if(counter<=10){
						score =10-counter;
					}else{}
					JOptionPane.showMessageDialog(null,"您总共猜了"+counter+"次     共花费了"+(endTime-startTime)/1000+"秒的时间猜到正确答案！  您的最后得分是"+score+"分！");
					int i=JOptionPane.showConfirmDialog(null, "你还要继续吗?", "提示",JOptionPane.YES_NO_OPTION);
					if(i==JOptionPane.NO_OPTION){
						System.exit(0);
					}else{
						
					}
				}
				
		}
	}
	
		
}

public class GuessNumber{
	public static void main(String args[]) {
		WindowButton win = new WindowButton();
	}
}        
